package com.example.linearlayoutdemo.network.auth.viewmodel;
import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.example.linearlayoutdemo.network.auth.model.LoginResponse;
import com.example.linearlayoutdemo.network.auth.model.User;
import com.example.linearlayoutdemo.network.auth.repository.AuthRepository;
import com.example.linearlayoutdemo.network.auth.auth.ApiService;

/**
 * 登录ViewModel，处理登录相关业务逻辑
 */
public class LoginViewModel extends AndroidViewModel {
    private final AuthRepository authRepository;

    // 用于观察的LiveData
    public final MutableLiveData<Boolean> isLoading = new MutableLiveData<>();
    public final MutableLiveData<String> errorMessage = new MutableLiveData<>();
    public final MutableLiveData<Boolean> loginSuccess = new MutableLiveData<>();
    public final MutableLiveData<User> currentUser = new MutableLiveData<>();

    public LoginViewModel(@NonNull Application application) {
        super(application);
        authRepository = AuthRepository.getInstance(application);
    }

    /**
     * 执行登录操作
     */
    public void login(String username, String password, boolean isAutoLogin) {
        // 输入验证
        if (username.isEmpty() || password.isEmpty()) {
            errorMessage.setValue("用户名和密码不能为空");
            return;
        }

        isLoading.setValue(true);
        User user = new User(username, password);

        authRepository.login(user, new ApiService.ApiCallback<LoginResponse>() {
            @Override
            public void onStart() {
                isLoading.setValue(true);
            }

            @Override
            public void onSuccess(LoginResponse response) {
                if (response.isSuccess() && response.getUser() != null) {
                    // 保存用户信息
                    authRepository.saveUser(response.getUser(), isAutoLogin);
                    currentUser.setValue(response.getUser());
                    loginSuccess.setValue(true);
                } else {
                    errorMessage.setValue(response.getMessage() != null ? response.getMessage() : "登录失败");
                }
            }

            @Override
            public void onFailure(Exception e) {
                errorMessage.setValue(e.getMessage() != null ? e.getMessage() : "登录失败，请检查网络");
            }

            @Override
            public void onComplete() {
                isLoading.setValue(false);
            }
        });
    }

    /**
     * 检查是否可以自动登录
     */
    public boolean canAutoLogin() {
        return authRepository.canAutoLogin();
    }

    /**
     * 执行自动登录
     */
    public void autoLogin() {
        isLoading.setValue(true);

        authRepository.autoLogin(new ApiService.ApiCallback<LoginResponse>() {
            @Override
            public void onStart() {
                isLoading.setValue(true);
            }

            @Override
            public void onSuccess(LoginResponse response) {
                if (response.isSuccess() && response.getUser() != null) {
                    // 更新保存的用户信息
                    authRepository.saveUser(response.getUser(), true);
                    currentUser.setValue(response.getUser());
                    loginSuccess.setValue(true);
                } else {
                    errorMessage.setValue("自动登录失败，请手动登录");
                }
            }

            @Override
            public void onFailure(Exception e) {
                errorMessage.setValue("自动登录失败：" + e.getMessage());
            }

            @Override
            public void onComplete() {
                isLoading.setValue(false);
            }
        });
    }
}

